package com.tomek.ktoryoperator.tasks;

import java.util.Arrays;
import java.util.Map;

import android.content.ContentResolver;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

import com.tomek.ktoryoperator.exceptions.NumberInfoProviderException;
import com.tomek.ktoryoperator.model.DBEntry;
import com.tomek.ktoryoperator.providers.NumberInfoProvider;
import com.tomek.ktoryoperator.utils.ContentProvidersUtils;
import com.tomek.ktoryoperator.utils.LogUtils;
import com.tomek.ktoryoperator.utils.NotificationUtils;
import com.tomek.ktoryoperator.utils.PreferenceUtils;

public class GetInfosForNumbersTask extends AsyncTask<String, Void, String[]> {
	final Context context;
	final Map<String, NumberInfoProvider> numberInfoProviders;

	public GetInfosForNumbersTask(Context context, Map<String, NumberInfoProvider> numberInfoProviders) {
		this.context = context;
		this.numberInfoProviders = numberInfoProviders;
	}

	@Override
	protected String[] doInBackground(String... numbers) {
		LogUtils.d(getClass(), "doInBackground", "numbers=%s", Arrays.asList(numbers));

		for (int i = 0; i < numbers.length; i++) {
			String number = numbers[i];

			String numberInfoId = null;

			for (NumberInfoProvider infoProvider : this.numberInfoProviders.values()) {
				try {
					numberInfoId = infoProvider.getNumberInfoId(number);

					if (numberInfoId != null) {
						saveAndNotify(number, numberInfoId, infoProvider.getProviderId(),
								infoProvider.getNumberInfoName(context, numberInfoId));
						break;
					}
				} catch (NumberInfoProviderException e) {
					LogUtils.e(e, "Blad podczas odczytu operatora");
				}
			}

			// no provider matched
			if (numberInfoId == null) {
				Looper.prepare();
				Handler h = new Handler();
				h.post(new Runnable() {

					@Override
					public void run() {
						Toast.makeText(context, "aaaaaaaaaaaaaaaaa", Toast.LENGTH_LONG).show();

					}
				});

			}
		}

		return numbers;
	}

	private void saveAndNotify(String number, String numberInfoId, String provider, String numberInfoName) {
		LogUtils.d(getClass(), "saveAndNotify", "number=%s, numberInfoId=%s, numberInfoName=%s", number, numberInfoId,
				numberInfoName);

		ContentResolver cr = this.context.getContentResolver();
		DBEntry entry = ContentProvidersUtils.load(cr, number);

		if (entry != null) {
			// jest wpis dla takiego numeru, update
			ContentProvidersUtils.updateEntry(cr, entry.id, provider, numberInfoId);

			if (!entry.value.equals(numberInfoId) && PreferenceUtils.notifyAboutNetworkOperatorChange(context)) {
				String contactName = ContentProvidersUtils.getContactNameForNumber(cr, number);

				NotificationUtils.showNotificationOperatorChanged(context, contactName, number, entry.value,
						numberInfoName);
			}
		} else {
			// nie ma takiego wpisu jeszcze, insert
			ContentProvidersUtils.insertEntry(cr, number, provider, numberInfoId);

			if (PreferenceUtils.notifyAboutFirstRead(context)) {
				String contactName = ContentProvidersUtils.getContactNameForNumber(cr, number);

				NotificationUtils.showNotificationOperatorAdded(context, contactName, number, numberInfoName);
			}
		}
	}
}